﻿using Microsoft.AspNetCore.Identity;
using System.Reflection.Emit;

namespace IdsDemo
{
    //public class IdpDbContext : IdentityDbContext<ApplicationUser>
    //{
    //    public IdpDbContext(DbContextOptions<IdpDbContext> opt) : base(opt)
    //    {

    //    }
    //    protected override void OnModelCreating(ModelBuilder builder)
    //    {
    //        base.OnModelCreating(builder);
    //        builder.Entity<ApplicationUser>().ToTable("ApplicationUsers");
    //        #region #
    //        //builder.Entity<IdentityUserLogin<string>>().ToTable("ApplicationLogins");
    //        //builder.Entity<IdentityUserClaim<string>>().ToTable("ApplicationUserClaims");
    //        //builder.Entity<ApplicationUserRole>().ToTable("ApplicationUserRoles");
    //        //builder.Entity<IdentityUserToken<string>>().ToTable("ApplicationUserTokens");
    //        //builder.Entity<ApplicationRole>().ToTable("ApplicationRoles");
    //        //builder.Entity<IdentityRoleClaim<string>>().ToTable("ApplicationRoleClaims");
    //        //builder.Entity<ApplicationUserRole>().HasKey(t => t.Id).HasName("PK_UserRole_ID_KEY");
    //        #endregion

    //        builder.Entity<ApplicationUser>().HasData(
    //            new ApplicationUser()
    //            {
    //                Id = Guid.NewGuid().ToString(),
    //                RealName = "alice1",
    //                UserName = "alice1",
    //                PasswordHash = "alice1"
    //            });
    //        #region 初始化用戶与角色的种子数据
    //        //1. 更新用戶与角色的外鍵
    //        builder.Entity<ApplicationUser>(
    //            u => u.HasMany(x => x.UserRoles).WithOne().HasForeignKey(ur => ur.UserId).IsRequired()
    //            );
    //        //2. 添加管理员角色
    //        var adminRoleId = "f8df1775-e889-46f4-acdd-421ec8d9ba64";
    //        builder.Entity<IdentityRole>().HasData(
    //            new IdentityRole()
    //            {
    //                Id = adminRoleId,
    //                Name = "Admin",
    //                NormalizedName = "Admin".ToUpper()
    //            }
    //        );
    //        //3. 添加用户
    //        var adminUserId = "f8df1775-e889-46f4-acdd-421ec8d9ba65";
    //        ApplicationUser adminUser = new ApplicationUser
    //        {
    //            Id = adminUserId,
    //            UserName = "admin",
    //            NormalizedUserName = "admin".ToUpper(),
    //            RealName = "admin",
    //            NormalizedEmail = "admin@qq.com".ToUpper(),
    //            Email = "admin@qq.com",
    //            TwoFactorEnabled = false,
    //            EmailConfirmed = true,
    //            PhoneNumber = "123456789",
    //            PhoneNumberConfirmed = false,

    //        };
    //        MyPasswordHasher ph = new MyPasswordHasher();
    //        adminUser.PasswordHash = ph.HashPassword(adminUser, "123456");
    //        builder.Entity<ApplicationUser>().HasData(adminUser);
    //        //4. 给用户加入管理员角色
    //        builder.Entity<IdentityUserRole<string>>().HasData(
    //            new IdentityUserRole<string>()
    //            {
    //                RoleId = adminRoleId,
    //                UserId = adminUserId
    //            }
    //            );
    //        #endregion

    //    }
    //}
}
